import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.lang.Object;
/**
 * Write a description of class PipeLineCurved here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PipeLineCurved extends Actor
{
    /*
     * Scale pipeline to normal propoties
     */
    public PipeLineCurved(int width, int height) 
    {
        GreenfootImage image = getImage();
        image.scale(width, height);
        setImage(image);
    }
    /**
     * Act - do whatever the PipeLineCurved wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        /*
         * Check only the pipeline that is clicked on
         */
        if(Greenfoot.mouseClicked(this) && ((MiniGame)getWorld()).getGameStatus().toString() == "PLAYING") {  
            /*
             * Play sound on mouseclick
             */
            GreenfootSound click = new GreenfootSound("button-3.mp3");
            click.setVolume(50);
            click.play();
            
            turn(90);
            
            /*
             * Get mouse information
             */
            MouseInfo mouse = Greenfoot.getMouseInfo();
            
            /*
             * Get background actor of the pipeline (to make it active)
             */
            List BackgroundObject = getWorld().getObjectsAt(mouse.getActor().getX(), mouse.getActor().getY(), TileBackground.class);
            Actor Background = (TileBackground)BackgroundObject.get(0); 
            
            /*
             * Set first pipeline to active if it is rotated correctly (first PipeLine is curved
             */
            if(mouse.getActor().getX() == 50 && mouse.getActor().getY() == 150 && mouse.getActor().getRotation() == 0) {
                this.changeBg(Background);
            } else {
                /*
                 * Check rotation of this actor
                 */
                if(this.getRotation() == 0) {
                    /*
                     * Check if pipeline neighbours are active and what location they are. Also checks what rotation the neighbour has
                     */
                    if(this.getNeighbour().equals("CR180") || this.getNeighbour().equals("CR270")) {
                        /*
                         * This pipeline is joining correctly to a active neighbour
                         *      Set the background to active state
                         */
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("CT90") || this.getNeighbour().equals("CT180")) {
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("SR0") || this.getNeighbour().equals("SR180")) {
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("ST90") || this.getNeighbour().equals("ST270")) {
                        this.changeBg(Background);
                    }
                } 
                 /*
                  * Check if pipeline neighbours are active and what location they are. Also checks what rotation the neighbour has
                  */
                else if(this.getRotation() == 90) {
                    if(this.getNeighbour().equals("CB0") || this.getNeighbour().equals("CB270")) {
                         /*
                         * This pipeline is joining correctly to a active neighbour
                         *      Set the background to active state
                         */
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("CR180") || this.getNeighbour().equals("CR270")) {
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("SR0") || this.getNeighbour().equals("SR180")) {
                        this.changeBg(Background);                     
                    } else if(this.getNeighbour().equals("SB90") || this.getNeighbour().equals("SB270")) {
                        this.changeBg(Background);
                    } else {
                        /*
                         * This pipeline is NOT joining correctly to a active neighbour
                         *      Turn this pipeline 90 degrees
                         */
                        //turn(90);
                    }
                } 
                /*
                 * Check if pipeline neighbours are active and what location they are. Also checks what rotation the neighbour has
                 */
                else if(this.getRotation() == 180) {
                    if(this.getNeighbour().equals("CL0") || this.getNeighbour().equals("CL90")) {
                        /*
                         * This pipeline is joining correctly to a active neighbour
                         *      Set the background to active state
                         */
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("CB0") || this.getNeighbour().equals("CB270")) {
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("SL0") || this.getNeighbour().equals("SL180")) {
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("SB90") || this.getNeighbour().equals("SB270")) {
                        this.changeBg(Background);
                    } else {
                        /*
                         * This pipeline is NOT joining correctly to a active neighbour
                         *      Turn this pipeline 90 degrees
                         */
                        //turn(90);
                    }
                }
                /*
                 * Check if pipeline neighbours are active and what location they are. Also checks what rotation the neighbour has
                 */
                else if(this.getRotation() == 270) {
                    if(this.getNeighbour().equals("CL0") || this.getNeighbour().equals("CL90")) {
                        /*
                         * This pipeline is joining correctly to a active neighbour
                         *      Set the background to active state
                         */
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("CT90") || this.getNeighbour().equals("CT180")) {
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("SL0") || this.getNeighbour().equals("SL180")) {
                        this.changeBg(Background);
                    } else if(this.getNeighbour().equals("ST90") || this.getNeighbour().equals("ST270")) {
                        this.changeBg(Background);
                    } else {
                        /*
                         * This pipeline is NOT joining correctly to a active neighbour
                         *      Turn this pipeline 90 degrees
                         */
                        //turn(90);
                    }
                }
            }
        }
    }     
    public String getNeighbour() 
    {
        /*
         * Get mouse info 
         */
        MouseInfo mouse = Greenfoot.getMouseInfo();  
        /*
         * Neighbour object is one tile above this actor
         */
        List objectstop = getWorld().getObjectsAt(mouse.getActor().getX(), mouse.getActor().getY()-100, null);
        for(Object object : objectstop) {
            if(object.getClass() == PipeLineStraight.class) {
                Actor PipeLineStraight = (PipeLineStraight)object;
                if(isActive(getWorld().getObjectsAt(PipeLineStraight.getX(), PipeLineStraight.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "ST" + PipeLineStraight.getRotation();
                }
            } else if(object.getClass() == PipeLineCurved.class) {
                Actor PipeLineCurved = (PipeLineCurved)object;
                if(isActive(getWorld().getObjectsAt(PipeLineCurved.getX(), PipeLineCurved.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "CT" + PipeLineCurved.getRotation();
                }
            }
        }     
        /*
         * Neighbour object is one tile below this actor
         */
        List objectsbottom = getWorld().getObjectsAt(mouse.getActor().getX(), mouse.getActor().getY()+100, null);
        for(Object object : objectsbottom) {
            if(object.getClass() == PipeLineStraight.class) {
                Actor PipeLineStraight = (PipeLineStraight)object;
                if(isActive(getWorld().getObjectsAt(PipeLineStraight.getX(), PipeLineStraight.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "SB" + PipeLineStraight.getRotation();
                }
            } else if(object.getClass() == PipeLineCurved.class) {
                Actor PipeLineCurved = (PipeLineCurved)object;
                if(isActive(getWorld().getObjectsAt(PipeLineCurved.getX(), PipeLineCurved.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "CB" + PipeLineCurved.getRotation();
                }
            }
        }   
        /*
         * Neighbour object is one tile on the left of this actor
         */
        List objectsleft = getWorld().getObjectsAt(mouse.getActor().getX()-100, mouse.getActor().getY(), null);
        for(Object object : objectsleft) {
            if(object.getClass() == PipeLineStraight.class) {
                Actor PipeLineStraight = (PipeLineStraight)object;
                if(isActive(getWorld().getObjectsAt(PipeLineStraight.getX(), PipeLineStraight.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "SL" + PipeLineStraight.getRotation();
                }
            } else if(object.getClass() == PipeLineCurved.class) {
                Actor PipeLineCurved = (PipeLineCurved)object;
                if(isActive(getWorld().getObjectsAt(PipeLineCurved.getX(), PipeLineCurved.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "CL" + PipeLineCurved.getRotation();
                }
            }
        }   
        /*
         * Neighbour object is one tile on the right of this actor
         */
        List objectsright = getWorld().getObjectsAt(mouse.getActor().getX()+100, mouse.getActor().getY(), null);
        for(Object object : objectsright) {
            if(object.getClass() == PipeLineStraight.class) {
                Actor PipeLineStraight = (PipeLineStraight)object;
                if(isActive(getWorld().getObjectsAt(PipeLineStraight.getX(), PipeLineStraight.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "SR" + PipeLineStraight.getRotation();
                }
            } else if(object.getClass() == PipeLineCurved.class) {
                Actor PipeLineCurved = (PipeLineCurved)object;
                if(isActive(getWorld().getObjectsAt(PipeLineCurved.getX(), PipeLineCurved.getY(), TileBackground.class))) {
                    /*
                     * This Neighbour is active and has class PipeLineCurved. 
                     *       Returns neighbours location and rotation
                     */
                    return "CR" + PipeLineCurved.getRotation();
                }
            }
        }   
        return " ";
    }
    /*
     * Check if the neighbour of this pipeline has underlying TileBackground actor which is active
     */
    public boolean isActive(java.util.List objects) 
    {
        for(Object object : objects) {
            /*
             * workaround to get image file url, no default function within greenfoot
             */
            Actor TileBackground = (TileBackground)object;
            TileBackground.getImage();
            String[] imageOriginal = TileBackground.getImage().toString().split("Image file name: ");
            String[] imageOriginalParts = imageOriginal[1].split("Image url: ");
            
            
            if(imageOriginalParts[0].trim().equals("PipeLines/TileBackgroundActive.png")) {
                /*
                 * TileBackground of neighbours pipeline is active
                 */
                return true;
            } else {
                /*
                 * TileBackground of neighbours pipeline is NOT active
                 */
                return false;
            }
        }
        return false;
    }
    /*
     * function to change the image of the TileBackground class which lays behind the pipeline 
     */
    public void changeBg(java.lang.Object objects) 
    {
        Actor TileBackground = (TileBackground)objects;
        TileBackground.setImage("PipeLines/TileBackgroundActive.png");
        GreenfootImage image = TileBackground.getImage();
        image.scale(100, 100);
        if(TileBackground.getX() == 650 && TileBackground.getY() == 550) {
            GreenfootSound  applause = new GreenfootSound("applause-01.mp3");
            applause.setVolume(50);
            applause.play();
            ((MiniGame)getWorld()).gameWon();
        }
    }
}
